package com.poker.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.poker.base.MyRealm;
import com.poker.base.Result;
import com.poker.base.ResultCode;
import com.poker.dao.SeatedUserMapper;
import com.poker.dao.PlayerMapper;
import com.poker.entity.SeatedUser;
import com.poker.service.PlayService;
import com.poker.websocket.SelectWebSocket;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;

@Service
public class PlayServiceImpl implements PlayService {

    @Autowired
    private PlayerMapper playerMapper;
    @Autowired
    private SelectWebSocket selectWebSocket;
    @Autowired
    private SeatedUserMapper seatedUserMapper;

    @Override
    public Result<Integer> selectSeat(String seatNum) throws IOException {
        String userId = MyRealm.getUserId();
        //查看是不是已经完成选座
        if (null != seatedUserMapper.selectById(userId)) {
            return Result.error(ResultCode.NORMAL_ERROR,"您已经选过座了");
        }
        SeatedUser newObj = new SeatedUser();
        newObj.setUserId(userId);
        newObj.setSeatNum(Integer.valueOf(seatNum));
        //如果是3号位，则为房主
        if ("3".equals(seatNum)){
            //如果选择了当房主，则提示用户成为房主
            newObj.setIsOwner(1);
            seatedUserMapper.insert(newObj);
            //进行消息广播
            SelectWebSocket.sendMessage("有人进入房间");
            return Result.success(seatedUserMapper.selectCount(null),"选座成功，您成为当前游戏的房主");
        }else{
            seatedUserMapper.insert(newObj);
            //进行消息广播
            SelectWebSocket.sendMessage("有人进入房间");
            return Result.success(seatedUserMapper.selectCount(null),"选座成功");
        }
    }

    @Override
    public Result<List<SeatedUser>> querySeated() {
        List<SeatedUser> seatedUsers = seatedUserMapper.selectAll();
        if (!CollectionUtils.isEmpty(seatedUsers)){
            for (SeatedUser lu: seatedUsers) {
                lu.setIcon(new String(lu.getPhoto()));
                lu.setPhoto(null);
            }
        }
        return Result.success(seatedUsers);
    }

    @Override
    public Result<List<SeatedUser>> querySides() {
        List<SeatedUser> seatedUsers = seatedUserMapper.selectAll();
        SeatedUser side1 = new SeatedUser();
        SeatedUser side2 = new SeatedUser();

        side1.setUserId(seatedUsers.get(0).getUserId() + "," + seatedUsers.get(2).getUserId());
        side2.setUserId(seatedUsers.get(1).getUserId() + "," + seatedUsers.get(3).getUserId());
        side1.setName(seatedUsers.get(0).getName() + " & " + seatedUsers.get(2).getName());
        side2.setName(seatedUsers.get(1).getName() + " & " + seatedUsers.get(3).getName());
        List<SeatedUser> result = new ArrayList<>();
        result.add(side1);
        result.add(side2);
        return Result.success(result);
    }

    @Override
    public Result<List<SeatedUser>> queryRecorders(String userIds) {
        String[] userIdList =  userIds.split(",");
        System.out.println("userIdList"+ JSON.toJSONString(userIdList));
        String sql = "('"+userIdList[0]+"','"+ userIdList[1]+"')";
        List<SeatedUser> result = seatedUserMapper.queryRecorders(sql);
        return Result.success(result);
    }
}
